package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Admin;
import com.example.entity.ShopCar;
import com.example.service.ShopCarService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shopCars")
public class ShopCarController {

    @Autowired
    private ShopCarService shopCarService;


    /**
     * 分页查询当前用户购物车信息
     * @return
     */
    @GetMapping("/getPageShopCar/{currentPage}/{pageSize}/{id}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,@PathVariable int id){
        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<ShopCar> lqw =new LambdaQueryWrapper<>();
        lqw.like(ShopCar::getUserId,id);

        IPage page1 = shopCarService.page(page,lqw);

        return R.success("分页查询当前用户购物车信息成功！",page1);
    }


    /**
     * 查询当前用户所有购物车信息
     * @param id
     * @return
     */
    @GetMapping("/getAll/{id}")
    public R getAll(@PathVariable int id){

        LambdaQueryWrapper<ShopCar> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShopCar::getUserId,id);
        List<ShopCar> list =shopCarService.list(lqw);

        return R.success("查询所有ShopCar!",list);
    }


    /**
     * 计算当前用户购物车总价
     * SELECT sum(total) as total FROM shop_car
     * @return
     */

    @GetMapping("/sumTotal/{id}")

    public R sumTotal(@PathVariable int id){

        QueryWrapper<ShopCar> queryWrapper = new QueryWrapper<ShopCar>();
        queryWrapper.select("sum(total) as total");
        queryWrapper.lambda().eq(ShopCar::getUserId,id);
        ShopCar total= shopCarService.getOne(queryWrapper);

        return R.success("分页查询所有shopCar成功！",total);
    }

    /**
     * 添加
     * @param shopCar
     * @return
     */
    @PostMapping("/addShopCar")
    public R add(@RequestBody ShopCar shopCar){

        Boolean update = shopCarService.save(shopCar);
        if (update){
            return R.success("添加shopCar成功!",null);
        }
        return R.error("添加shopCar失败!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteShopCar/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = shopCarService.removeById(id);
        if (byId){
            return R.success("删除一个shopCar成功!",null);
        }
        return R.error("删除一个shopCar失败!");
    }
    /**
     * 删除所有
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAllByUserId/{id}")
    public R deleteAllByUserId(@PathVariable int id){

        LambdaQueryWrapper<ShopCar> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShopCar::getUserId,id);
        Boolean byId = shopCarService.remove(lqw);
        if (byId){
            return R.success("删除id为"+id+"的所有shopCar成功!",null);
        }
        return R.error("删除id为"+id+"的所有shopCar失败!");
    }
//    /**
//     * 通过用户id删除
//     * @param id
//     * @return
//     */
//    @DeleteMapping("/deleteAllShopCarByUserId/{id}")
//    public R deleteAllShopCarByUserId(@PathVariable int id){
//
//        Boolean byId = shopCarService.deleteAllShopCarByUserId(id);
//        if (byId){
//            return R.success("删除当前用户所有shopCar成功!",null);
//        }
//        return R.error("删除当前用户所有shopCar失败!");
//    }
//
    /**
     * 修改
     * @param shopCar
     * @return
     */
    @PutMapping("/updateShopCar")
    public R deleteById(@RequestBody ShopCar shopCar){

        Boolean update = shopCarService.updateById(shopCar);
        if (update){
            return R.success("修改shopCar成功!",null);
        }
        return R.error("修改shopCar失败!");
    }



}

